package com.wcgksoft.controller.system;

import com.wcgksoft.common.util.DateUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.Enterprise;
import com.wcgksoft.entity.system.SysMenu;
import com.wcgksoft.entity.system.SysRole;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.SysRoleCodeService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import com.wcgksoft.service.system.SystemVersionService;
import net.sf.json.JSONObject;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author 阿良
 * @version 1.0.0
 * @ClassName EnterpriseController.java
 * @Description 企业控制器
 * @createTime 2020年03月11日 10:22:00
 */
@Controller
@RequestMapping("enterprise")
public class EnterpriseController {

@Autowired
private EnterpriseService enterpriseService;

@Autowired
private SystemVersionService systemVersionService;
@Autowired
private OpenAccountService openAccountService;
@Autowired
private SysRoleService sysRoleService;
@Autowired
private SysRoleCodeService sysRoleCodeService;

  /**
   * @throws
   * @title TODO
   * @description 企业列表表
   * @author 阿良
   * @param: page
   * @param: limit
   * @updateTime 2020/3/6 15:23
   * @return: com.wcgksoft.common.util.Layui
   */
  @ResponseBody
  @RequestMapping(value = "list")
  public Layui findAll(
          @RequestParam(value = "page", required = false) Integer page,
          @RequestParam(value = "limit", required = false) Integer limit,
          @RequestParam(value = "term",required = false,defaultValue = "") String search_term) {

    DetachedCriteria criteria = DetachedCriteria.forClass(Enterprise.class);
    if (!search_term.isEmpty()) {
      criteria.add(Restrictions.eq("departName", search_term));
    }
    List<Enterprise> list = enterpriseService.findByConditions(criteria, page, limit);
    return Layui.data(0, "查询成功", enterpriseService.getTotalCount(), list);

  }


  /**
   * @title TODO
   * @description 添加修改企业
   * @author 阿良
   * @param: request
   * @param: data
   * @param: meun
   * @updateTime 2020/3/11 10:31
   * @return: com.wcgksoft.common.util.Layui
   * @throws
   */
  @ResponseBody
  @RequestMapping(value = "update")
  public Layui update(HttpServletRequest request,
                      @RequestParam(value = "data", required = false) String data) throws Exception {
    JSONObject param = JSONObject.fromObject(data);
    if (param == null) {
      return Layui.data(1, "请求格式有误", 0, null);
    } else {
      Enterprise enterprise = (Enterprise) JSONObject.toBean(JSONObject.fromObject(data), Enterprise.class);
      enterprise.setSystemVersion(systemVersionService.findById(param.getInt("systemVersion")));
      enterprise.setCreateDate(DateUtil.parse(param.getString("CreateDate"),"YYYY-mm-dd"));
      enterprise.setCreateName(param.getString("CreateName"));
      enterpriseService.saveOrUpdate(enterprise);
      if (null == param.get("id")||"".equals(param.get("id"))){
        HashSet<SysMenu> set = new HashSet<>();
        for (SysMenu menu : enterprise.getSystemVersion().getSysMenuSet()) {
          if (menu.getIsLeader()==0) {
            set.add(menu);
          }
        }
        SysUser user = new SysUser();
        user.setPhone(param.getString("loginaccount"));
        user.setPassWord(param.getString("loginpsd"));
        user.setCreateDate(new Date());
        user.setName(enterprise.getDepartName());
        user.setEnterprise(enterprise);
        user.setCompanyName(enterprise.getDepartName());
        user.setStatus(1);
        SysRole role = new SysRole();
        role.setSysRoleCode(sysRoleCodeService.findById(2));
        role.setAdd_time(new Date());
        role.setEnterprise(enterprise);
        role.setSysMenuSet(set);
        sysRoleService.save(role);
        user.setSysRole(role);/*TODO 企业ID是二目前写死*/
        openAccountService.save(user);
      }
      return Layui.data(0, "保存成功", 1, Layui.addList(enterprise));
    }
  }
  /**
   * 删除
   *
   * @param id
   * @param
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "delete")
  @Transactional
  public Layui delete(@RequestParam(value = "id") Integer id) {
    Enterprise byId = enterpriseService.findById(id);
    enterpriseService.deleteCass(byId);

    return Layui.data(1, "删除成功", 1, null);
  }


}
